package com.manage.project.biz.controller;


import com.deepoove.poi.XWPFTemplate;
import com.manage.common.utils.poi.ExcelUtil;
import com.manage.framework.aspectj.lang.annotation.Log;
import com.manage.framework.aspectj.lang.enums.BusinessType;
import com.manage.framework.web.controller.BaseController;
import com.manage.framework.web.domain.AjaxResult;
import com.manage.framework.web.page.TableDataInfo;
import com.manage.project.biz.domain.BizJishupingding;
import com.manage.project.biz.domain.BizSuidao;
import com.manage.project.biz.domain.CivilEnginneeringTechnicalTotal;
import com.manage.project.biz.domain.TechnicalTotal;
import com.manage.project.biz.service.IBizJishupingdingService;
import com.manage.project.biz.service.IBizSuidaoService;
import com.manage.project.biz.service.ICivilEnginneeringTechnicalTotalService;
import com.manage.project.biz.service.ITechnicalTotalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


/**
 * 总体技术状况评定Controller
 *
 * @author 小赵
 * @date 2022-05-22
 */
@RestController
@RequestMapping("/technicaltotal/technicaltotal")
public class TechnicalTotalController extends BaseController
{


    static Calendar cal = Calendar.getInstance();
    static int day = cal.get(Calendar.DATE);
    static int month = cal.get(Calendar.MONTH) + 1;
    static int year = cal.get(Calendar.YEAR);
    @Autowired
    private ITechnicalTotalService technicalTotalService;
    @Autowired
    private ICivilEnginneeringTechnicalTotalService iCivilEnginneeringTechnicalTotalService;
    @Autowired
    private IBizJishupingdingService jishupingdingService;
    @Autowired
    private IBizSuidaoService iBizSuidaoService;
    /**
     * 查询总体技术状况评定列表
     */
    @PreAuthorize("@ss.hasPermi('technicaltotal:technicaltotal:list')")
    @GetMapping("/list")
    public TableDataInfo list(TechnicalTotal technicalTotal)
    {
        startPage();
        List<TechnicalTotal> list = technicalTotalService.selectTechnicalTotalList(technicalTotal);
        return getDataTable(list);
    }

    /**
     * 导出总体技术状况评定列表
     */
    @PreAuthorize("@ss.hasPermi('technicaltotal:technicaltotal:export')")
    @Log(title = "总体技术状况评定", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TechnicalTotal technicalTotal) throws IOException {
        List<TechnicalTotal> list = technicalTotalService.selectTechnicalTotalList(technicalTotal);
        ExcelUtil<TechnicalTotal> util = new ExcelUtil<TechnicalTotal>(TechnicalTotal.class);
        util.exportExcel(response, list, "总体技术状况评定数据");
    }

    /**
     * 获取总体技术状况评定详细信息
     */
    @PreAuthorize("@ss.hasPermi('technicaltotal:technicaltotal:query')")
    @GetMapping(value = "/{totalId}")
    public AjaxResult getInfo(@PathVariable("totalId") Long totalId)
    {
        return AjaxResult.success(technicalTotalService.selectTechnicalTotalByTotalId(totalId));
    }

    /**
     * 新增总体技术状况评定
     */
    @PreAuthorize("@ss.hasPermi('technicaltotal:technicaltotal:add')")
    @Log(title = "总体技术状况评定", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TechnicalTotal technicalTotal)
    {
        String tunnelName=technicalTotal.getTunnelName();
        String roadName=technicalTotal.getRoadName();
        String tunnelId=technicalTotal.getTunnelId();
        Date adjustDate=technicalTotal.getAdjustData();
        //将date类的日期转为String类型
        SimpleDateFormat strdate = new SimpleDateFormat("yyyy-MM-dd");
        String strDate= strdate.format(adjustDate);
     /*   System.out.println(strDate);*/
        BizSuidao bizSuidao= iBizSuidaoService.getByTunnelNameIdAndRoadName(roadName,tunnelName,tunnelId);
        if(bizSuidao==null){
            return new AjaxResult().error("当前公路名称、隧道名称和隧道编码不对应，请核对后重新输入");
        }
        CivilEnginneeringTechnicalTotal civilEnginneeringTechnicalTotal=
                iCivilEnginneeringTechnicalTotalService.selectByTunnelNameAndDate(tunnelName,adjustDate);
        BizJishupingding bizJishupingding=jishupingdingService.selectByTunnelNameAndDate(tunnelName,strDate);
        if(civilEnginneeringTechnicalTotal==null){
             return new AjaxResult().error("当前隧道还未进行土建结构技术状况评定，请先去评定");
        }else if(bizJishupingding==null){
            return new AjaxResult().error("当前隧道还未进行机电设施技术状况评定，请先去评定");
        } else {
            return toAjax(technicalTotalService.insertTechnicalTotal(technicalTotal));
        }
    }

    /**
     * 修改总体技术状况评定
     */
    @PreAuthorize("@ss.hasPermi('technicaltotal:technicaltotal:edit')")
    @Log(title = "总体技术状况评定", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TechnicalTotal technicalTotal)
    {
        String tunnelName=technicalTotal.getTunnelName();
        String roadName=technicalTotal.getRoadName();
        String tunnelId=technicalTotal.getTunnelId();
        Date adjustDate=technicalTotal.getAdjustData();
        //将date类的日期转为String类型
        SimpleDateFormat strdate = new SimpleDateFormat("yyyy-MM-dd");
        String strDate= strdate.format(adjustDate);
        /*   System.out.println(strDate);*/
        BizSuidao bizSuidao= iBizSuidaoService.getByTunnelNameIdAndRoadName(roadName,tunnelName,tunnelId);
        if(bizSuidao==null){
            return new AjaxResult().error("当前公路名称、隧道名称和隧道编码不对应，请核对后重新输入");
        }
        CivilEnginneeringTechnicalTotal civilEnginneeringTechnicalTotal=
                iCivilEnginneeringTechnicalTotalService.selectByTunnelNameAndDate(tunnelName,adjustDate);
        BizJishupingding bizJishupingding=jishupingdingService.selectByTunnelNameAndDate(tunnelName,strDate);
        if(civilEnginneeringTechnicalTotal==null){
            return new AjaxResult().error("当前隧道还未进行土建结构技术状况评定，请先去评定");
        }else if(bizJishupingding==null){
            return new AjaxResult().error("当前隧道还未进行机电设施技术状况评定，请先去评定");
        }else {
            return toAjax(technicalTotalService.updateTechnicalTotal(technicalTotal));
        }
    }

    /**
     * 删除总体技术状况评定
     */
    @PreAuthorize("@ss.hasPermi('technicaltotal:technicaltotal:remove')")
    @Log(title = "总体技术状况评定", businessType = BusinessType.DELETE)
	@DeleteMapping("/{totalIds}")
    public AjaxResult remove(@PathVariable Long[] totalIds)
    {
        return toAjax(technicalTotalService.deleteTechnicalTotalByTotalIds(totalIds));
    }

    /**
     * 导出总体技术状况评定为word文档
     */
    @PreAuthorize("@ss.hasPermi('technicaltotal:technicaltotal:exportWord')")
    @Log(title = "总体技术状况评定", businessType = BusinessType.EXPORT)
    @RequestMapping("/exportWord/{totalId}")
    public AjaxResult   exportWord(@PathVariable("totalId") Long totalId) throws IOException {
         TechnicalTotal technicalTotal= technicalTotalService.getByTotalId(totalId);
         String maintenance_organization=technicalTotal.getMaintenanceOrganization();
         String road_name=technicalTotal.getRoadName();
         String tunnel_name=technicalTotal.getTunnelName();
         String adjuster=technicalTotal.getAdjuster();
         String record=technicalTotal.getRecord();
         String tunnel_id=technicalTotal.getTunnelId();
         int tunnel_hole_limit=technicalTotal.getTunnelHoleLimit();
         int tunnel_hole_max=technicalTotal.getTunnelHoleMax();
         int tunnel_door_limit=technicalTotal.getTunnelDoorLimit();
         int tunnel_door_max=technicalTotal.getTunnelDoorMax();
         int linning_limit=technicalTotal.getLinningLimit();
         int lining_max=technicalTotal.getLiningMax();
         int jianxiudao_limit=technicalTotal.getJianxiudaoLimit();
         int jianxiudao_max=technicalTotal.getJianxiudaoMax();
         int water_limit=technicalTotal.getWaterLimit();
         int water_max=technicalTotal.getWaterMax();
         int roadsurface_limit=technicalTotal.getRoadsurfaceLimit();
         int roadsurface_max=technicalTotal.getRoadsurfaceMax();
         int direct_limit=technicalTotal.getDirectLimit();
         int direct_max=technicalTotal.getDirectMax();
         int marking_limit=technicalTotal.getMarkingLimit();
         int marking_max=technicalTotal.getMarkingMax();
         int top_limit=technicalTotal.getTopLimit();
         int top_max=technicalTotal.getTopMax();
         int civil_grade=technicalTotal.getCivilGrade();
         String civil_pclass=technicalTotal.getCivilPclass();
         int jd_grade=technicalTotal.getJdGrade();
         String jd_pclass=technicalTotal.getJdPclass();
         Long final_grade=technicalTotal.getFinalGrade();
         String final_pclass=technicalTotal.getFinalPclass();
         String suggest=technicalTotal.getSuggest();
         XWPFTemplate template = XWPFTemplate.compile("C:\\Users\\ZZT\\Desktop\\report.docx").render(
                new HashMap<String, Object>(){{
                    put("maintenance_organization", maintenance_organization);
                    put("road_name", road_name);
                    put("tunnel_name",tunnel_name);
                    put("adjuster",adjuster);
                    put("record",record);
                    put("year",year);
                    put("month",month);
                    put("day",day);
                    put("tunnel_id",tunnel_id);
                    put("tunnel_hole_limit",tunnel_hole_limit);
                    put("tunnel_hole_max",tunnel_hole_max);
                    put("tunnel_door_limit",tunnel_door_limit);
                    put("tunnel_door_max",tunnel_door_max);
                    put("linning_limit",linning_limit);
                    put("lining_max",lining_max);
                    put("jianxiudao_limit",jianxiudao_limit);
                    put("jianxiudao_max",jianxiudao_max);
                    put("water_limit",water_limit);
                    put("water_max",water_max);
                    put("roadsurface_limit",roadsurface_limit);
                    put("roadsurface_max", roadsurface_max);
                    put("direct_limit", direct_limit);
                    put("direct_max",direct_max);
                    put("marking_limit",marking_limit);
                    put("marking_max",marking_max);
                    put("top_limit",top_limit);
                    put("top_max",top_max);
                    put("civil_grade",civil_grade);
                    put("civil_pclass",civil_pclass);
                    put("jd_grade",jd_grade);
                    put("jd_pclass",jd_pclass);
                    put("final_grade",final_grade);
                    put("final_pclass",final_pclass);
                    put("suggest",suggest);
                }});
        try {
            template.writeAndClose(new FileOutputStream("C:\\Users\\ZZT\\Desktop\\技术状况评定报告.docx"));
        } catch (IOException e) {
            e.printStackTrace();
        }
         return AjaxResult.success("导出成功");
    }

}
